/*
 *  Copyright 2015 The WebRTC Project Authors. All rights reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

package com.cloudptt.wp.ui.call.single;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

import com.cloudptt.api.product.func.call.CallConst;
import com.cloudptt.api.product.func.call.single.LaunchCallInfo;
import com.cloudptt.wp.R;

/**
 * Fragment for call control.
 */
public class CallFragment extends Fragment {
	public static String THIS_FILE = "CallFragment";
	private View controlView;
	private ImageButton acceptButton;
	private ImageButton disconnectButton;
//	private ImageButton cameraSwitchButton;
//	private ImageButton videoScalingButton;
//	private ImageButton enableMicIb;
//	private ImageButton enableSpeakerIb;
//	private TextView captureFormatText;
//	private SeekBar captureFormatSlider;
	private OnCallEvents callEvents;
//	private ScalingType scalingType;
//	private int callType = CallAttr.CallType_Private;
	private boolean videoCallEnabled = true;
//	//  private WebRtcParameters para = null;
//	private boolean isCalled = false;
//	private int callState = CallConst.CallState_InitState;
//	private boolean isMicEnabled = true;
	private LaunchCallInfo callInfo = null;

	private RelativeLayout mAcceptRLayout;
	private RelativeLayout mDisconnectRLayout;
//	private RelativeLayout mCameraRLayout;
//	private RelativeLayout mVideoScalingRLayout;
//	private RelativeLayout mToggleMuteRLayout;
	private int curCallState = CallConst.CallState_InitState;
	private boolean enableMic = true;
	private boolean enableSpeaker = false;

	public CallFragment(LaunchCallInfo callInfo)
	{
		this.callInfo = callInfo;
	}

	/**
	 * Call control interface for container activity.
	 */
	public interface OnCallEvents {
		public void acceptCall();
		public void hangupCall();
		public void onCameraSwitch();
//		public void onVideoScalingSwitch(ScalingType scalingType);
//		public void onCaptureFormatChange(int width, int height, int framerate);
		public boolean setSpeakerphoneEnable(boolean isOn);
		public boolean setMicrophoneEnable(boolean isOn);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
		Log.d(THIS_FILE, "onCreateView");
		controlView =
				inflater.inflate(R.layout.fragment_call, container, false);

		// Create UI controls.
		mAcceptRLayout = 
				(RelativeLayout) controlView.findViewById(R.id.rl_accept);
		acceptButton =
				(ImageButton) controlView.findViewById(R.id.button_call_accept);
		mDisconnectRLayout =
				(RelativeLayout) controlView.findViewById(R.id.rl_disconnect);
		disconnectButton =
				(ImageButton) controlView.findViewById(R.id.button_call_disconnect);
//		mCameraRLayout =
//				(RelativeLayout) controlView.findViewById(R.id.rl_switch_camera);
//		cameraSwitchButton =
//				(ImageButton) controlView.findViewById(R.id.button_call_switch_camera);
//		if(callInfo != null
//				&& callInfo.isHasVideo())
//		{
//			cameraSwitchButton.setVisibility(View.VISIBLE);
//		}
//		else
//		{
//			cameraSwitchButton.setVisibility(View.INVISIBLE);
//		}
//
//		enableMicIb =
//				(ImageButton) controlView.findViewById(R.id.enableMicIb);
//		enableMicIb.setEnabled(enableMic);
//		enableMicIb.setVisibility(View.INVISIBLE);

//		enableSpeakerIb =
//				(ImageButton) controlView.findViewById(R.id.enableSpeakerIb);
//		enableSpeakerIb.setEnabled(enableSpeaker);

		// Add buttons click events.
		acceptButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				callEvents.acceptCall();
			}
		});
		//    if(isCalled)
		//    {
		//    	disconnectButton.setEnabled(false);
		//    }
		disconnectButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				callEvents.hangupCall();
			}
		});

//		cameraSwitchButton.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View view) {
//				callEvents.onCameraSwitch();
//			}
//		});

//		videoScalingButton.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View view) {
//				if (scalingType == ScalingType.SCALE_ASPECT_FILL) {
//					videoScalingButton.setBackgroundResource(
//							R.drawable.ic_action_full_screen);
//					scalingType = ScalingType.SCALE_ASPECT_FIT;
//				} else {
//					videoScalingButton.setBackgroundResource(
//							R.drawable.ic_action_return_from_full_screen);
//					scalingType = ScalingType.SCALE_ASPECT_FILL;
//				}
//				callEvents.onVideoScalingSwitch(scalingType);
//			}
//		});
//		scalingType = ScalingType.SCALE_ASPECT_FILL;
//		int callType = this.callInfo.getCallType();
//		if(callType == CallAttr.CallType_Webrtc_Single)
//		{
////			boolean isMicEnabled = this.callInfo.isMicEnabled();
////			toggleMuteButton.setSelected(!isMicEnabled);
//			boolean enabled = callEvents.isMicEnable();
//			toggleMuteButton.setSelected(!enabled);
//		}
//		else
//		{
//			toggleMuteButton.setEnabled(false);
//		}
		
//		enableMicIb.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View view) {
//				enableMic = !enableMic;
//				enableMic = callEvents.setMicrophoneEnable(enableMic);
//				enableMicIb.setSelected(!enableMic);
//			}
//		});

		return controlView;
	}

	@Override
	public void onStart() {
		super.onStart();
		Log.d(THIS_FILE, "onStart callInfo = " + callInfo);
		boolean captureSliderEnabled = false;
		//    String contactName = args.getString(CallActivity.EXTRA_IP);
		//    contactView.setText(contactName);
		//    videoCallEnabled = args.getBoolean(CallActivity.EXTRA_VIDEO_CALL, true);
		//    videoCallEnabled = para.isVideoCallEnabled();
		captureSliderEnabled = false;//videoCallEnabled && para.isCaptureQualitySlider();

		
//		if (!videoCallEnabled) {
//			cameraSwitchButton.setVisibility(View.INVISIBLE);
//		}
		if (captureSliderEnabled) {
//			captureFormatSlider.setOnSeekBarChangeListener(
//					new CaptureQualityController(captureFormatText, callEvents));
		}

		int callState = callInfo.getCallState();
		Log.d(THIS_FILE, "onStart callState = " + callState);
		updateView(callState);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		callEvents = (OnCallEvents) activity;
	}

	public void updateView(int callState)
	{
		if(callState == CallConst.CallState_Activite)
		{
			this.viewOnConnected();
		}
		else
		{
			if(callInfo.isMoCall())
			{
				viewOnCalling();
			}
			else
			{
				viewOnIncoming();
			}
		}
	}

	public void viewAfterOffer()
	{
		disconnectButton.setEnabled(true);

	}
	private void viewOnCalling()
	{
		//acceptButton.setVisibility(View.GONE);
		//disconnectButton.setVisibility(View.VISIBLE);
		//cameraSwitchButton.setVisibility(View.GONE);
		//toggleMuteButton.setVisibility(View.GONE);

		if(mAcceptRLayout != null)
		{
		mAcceptRLayout.setVisibility(View.GONE);
		}
		mDisconnectRLayout.setVisibility(View.VISIBLE);
//		mCameraRLayout.setVisibility(View.GONE);
//		mToggleMuteRLayout.setVisibility(View.GONE);

	}

	private void viewOnIncoming()
	{
		/*acceptButton.setVisibility(View.VISIBLE);
	  disconnectButton.setVisibility(View.VISIBLE);
	  cameraSwitchButton.setVisibility(View.GONE);
	  toggleMuteButton.setVisibility(View.GONE);*/
		if(mAcceptRLayout != null) {
			mAcceptRLayout.setVisibility(View.VISIBLE);
		}
		mDisconnectRLayout.setVisibility(View.VISIBLE);
//		mCameraRLayout.setVisibility(View.GONE);
//		mToggleMuteRLayout.setVisibility(View.GONE);
	}

	public void viewOnConnected()
	{
		/*acceptButton.setVisibility(View.GONE);
	  disconnectButton.setVisibility(View.VISIBLE);
	  cameraSwitchButton.setVisibility(View.VISIBLE);
	  toggleMuteButton.setVisibility(View.VISIBLE);*/

		if(mAcceptRLayout != null) {
			mAcceptRLayout.setVisibility(View.GONE);
		}
		mDisconnectRLayout.setVisibility(View.VISIBLE);
//		mCameraRLayout.setVisibility(View.VISIBLE);
//		mToggleMuteRLayout.setVisibility(View.VISIBLE);
	}
	
	public void setCallState(LaunchCallInfo callInfo) {
		Log.d(THIS_FILE, "setCallState callInfo = " + callInfo);
		if(callInfo != null)
		{
			this.callInfo = callInfo;
			int callState = callInfo.getCallState();
			Log.d(THIS_FILE, "setCallState callState = " + callState + " curCallState = " + curCallState);
			if(curCallState <= callState) {
				curCallState = callState;
				updateView(callState);
			}
		}
	}
}
